package com.amos.muexamclient.security.service.impl;

import com.amos.muexamclient.security.service.SysPermissionService;
import com.amos.muexamclient.sys.service.SysMenuService;
import com.amos.muexamclient.sys.service.SysRoleService;
import com.amos.muexamclient.sys.service.dto.SysUserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.Set;

/**
 * Copyright © 2018 嘉源锐信. All rights reserved.
 *
 * @Project: mu-exam
 * @ClassName: SysPermissionServiceImpl
 * @Package: com.amos.muexamclient.security.service.impl
 * @author: zhuqb
 * @Description:
 * @date: 2020/3/25 0025 下午 16:59
 * @Version: V1.0
 */
@Service
public class SysPermissionServiceImpl implements SysPermissionService {
    @Autowired
    private SysRoleService roleService;

    @Autowired
    private SysMenuService menuService;

    @Override
    public Set<String> getRolePermission(SysUserDTO user) {
        Set<String> roles = new HashSet<String>();
        // 管理员拥有所有权限
        if (user.isAdmin()) {
            roles.add("admin");
        } else {
            roles.addAll(roleService.selectRolePermissionByUserId(user.getId()));
        }
        return roles;
    }

    @Override
    public Set<String> getMenuPermission(SysUserDTO user) {
        Set<String> roles = new HashSet<String>();
        // 管理员拥有所有权限
        if (user.isAdmin()) {
            roles.add("*:*:*");
        } else {
            roles.addAll(menuService.selectMenuPermsByUserId(user.getId()));
        }
        return roles;
    }
}
